package com.sgr.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.sgr.dao.FuncionarioDao;
import com.sgr.model.Funcionario;

public class FuncionarioDaoImpl implements FuncionarioDao {
	
	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public Session getCurrentSession(){
		return this.sessionFactory.getCurrentSession();
	}

	@Override
	@Transactional
	public void save(Funcionario func) {
		getCurrentSession().save(func);
	}

	@Override
	@Transactional
	public void update(Funcionario func) {
		getCurrentSession().update(func);
	}

	@Override
	@Transactional
	public void delete(Funcionario func) {
		getCurrentSession().delete(func);
	}

	@Override
	@Transactional
	public List findByName(String name) {
		return null;
	}

	@Override
	@Transactional
	public Long count() {
		Long rows = (Long) getCurrentSession().createCriteria(Funcionario.class).setProjection(Projections.rowCount()).uniqueResult();
		return rows;
	}

	@Override
	@Transactional
	public List findAll() {
		List funcionarios = getCurrentSession().createQuery("from Funcionario").list();
		return funcionarios;
	}

	@Override
	@Transactional
	public Funcionario findByPk(int id) {
		Funcionario funcionario =  (Funcionario) getCurrentSession().createCriteria(Funcionario.class).add(Restrictions.eq("id", id)).uniqueResult();
		return funcionario;
	}

}
